/* 
 * EbayJSHelper Javascript Library v0.8.1
 * 
 * Coypright 2012, Jin Yu
 * 
 * Date: Sat Feb 18 13:17:32 2012 +1000
 */
var e$,ebayJSHelper;
(function(win){
    var doc = win.document,
    nav = win.navigator,
    loc = win.location;
    var appName={
        ie:'Microsoft Internet Explorer',
        netscape:'Netscape',
        opera:'Opera'
    }
    var browserName={
        ie:'MSIE',
        firefox:'Firefox',
        safari:'Safari',
        opera:'Opera',
        chrome:'Chrome'
    }
    var eventStack={
        version:'1.0'
    }
    var ralpha = /alpha\([^)\d\-]*(\-?\d+)\)/i;
    var itom = /([A-Z])/g,mtoi = /-([a-z])/g;
    var idPool = 0;
    var borderSet = ['borderTop','borderRight','borderBottom','borderLeft'];
    var border = ['borderTopWidth','borderRightWidth','borderBottomWidth','borderLeftWidth'];
    var padding = ['paddingTop','paddingRight','paddingBottom','paddingLeft']
    var margin = ['marginTop','marginRight','marginBottom','marginLeft']
    var borderAll = ['border-top-width','border-right-width','border-bottom-width','border-left-width'];
    var paddingAll = ['padding-top','padding-right','padding-bottom','padding-left']
    var marginAll = ['margin-top','margin-right','margin-bottom','margin-left']
    ebayJSHelper = e$ = {
        JSReady:false,
        ProductId:'unknown',
        ProductType:'unknown',
        ie:nav.appName==appName.ie,
        getcsstest:function(obj){
            if(e$.ie){
                e$.println("border:",e$.getComCss(obj, border[0]),e$.getComCss(obj, border[1]),e$.getComCss(obj, border[2]),e$.getComCss(obj, border[3])); 
                e$.println("padding:",e$.getComCss(obj, padding[0]),e$.getComCss(obj, padding[1]),e$.getComCss(obj, padding[2]),e$.getComCss(obj, padding[3])); 
                e$.println("margin:",e$.getComCss(obj, margin[0]),e$.getComCss(obj, margin[1]),e$.getComCss(obj, margin[2]),e$.getComCss(obj, margin[3])); 
            }else{
                e$.println("border:",e$.getComCss(obj, e$.cssItoM(borderAll[0])),e$.getComCss(obj, e$.cssItoM(borderAll[1])),e$.getComCss(obj, e$.cssItoM(borderAll[2])),e$.getComCss(obj, e$.cssItoM(borderAll[3]))); 
                e$.println("padding:",e$.getComCss(obj, e$.cssItoM(paddingAll[0])),e$.getComCss(obj, e$.cssItoM(paddingAll[1])),e$.getComCss(obj, e$.cssItoM(paddingAll[2])),e$.getComCss(obj, e$.cssItoM(paddingAll[3]))); 
                e$.println("margin:",e$.getComCss(obj, e$.cssItoM(marginAll[0])),e$.getComCss(obj, e$.cssItoM(marginAll[1])),e$.getComCss(obj, e$.cssItoM(marginAll[2])),e$.getComCss(obj, e$.cssItoM(marginAll[3]))); 
            }
           
        },
        getUniqueId:function(){
            var id = "";
            for (var i=0; i<6; i++){
                id+=String.fromCharCode(Math.random()*26 + 65);
            }
            id += "0000000000";
            var num = String(idPool++);
            return id.replace(new RegExp("0{"+num.length+"}$"), num);
        },
        is_ie:function(){
            return nav.appName==appName.ie;
        },
        is_ready:function(){
            return this.JSReady;
        },
        getProductId:function(){
            return this.ProductId;
        },
        getProductType:function(){
            return this.ProductType;
        },
        getById:function(id){
            return doc.getElementById(id);
        },
        getByTag:function(tag,obj){
            return obj.getElementsByTagName(tag);
        },
        getFlash:function(id){
            if(nav.userAgent.indexOf(browserName.safari)!=-1){
                return win[id][1];
            }else{
                return doc[id];
            }
        },
        getByClass:function(className,obj){
            var parent = obj?obj:doc;
            if (parent.getElementsByClassName == undefined) {
                var hasClassName = new RegExp("(?:^|\\s)" + className + "(?:$|\\s)");
                var allElements = parent.getElementsByTagName("*");
                var results = [];
                var element;
                for (var i = 0; (element = allElements[i]) != null; i++) {
                    var elementClass = element.className;
                    if (elementClass && elementClass.indexOf(className) != -1 && hasClassName.test(elementClass))
                        results.push(element);
                }
                return results;
            }else{
                return parent.getElementsByClassName(className);
            }
        },
        makeElem:function(tagName){
            return doc.createElement(tagName);
        },
        getOffset:function(obj){
            var offset = new Object();
            offset.left = offset.top = 0;
	
            if(obj.offsetParent){
                do{
                    offset.left += obj.offsetLeft;
                    offset.top += obj.offsetTop;
                }while(obj = obj.offsetParent);
            }
            return offset;
        },
        ajaxFeedFromFlash:function(obj){
            for (var name in obj){
                e$.println(name+":"+obj[name]);
            }
    
        },
        filterElems:function(arr){
            var newArr= [];
            for(var i=0; i<arr.length; i++){
                if(arr[i].nodeType && arr[i].nodeType=="1"){
                    newArr.push(arr[i]);
                }
            }
            return newArr;
        },
        enlargImg:function(imgs,relObj,engImg,scale){
            scale = scale||1.6;
            for(var i=0; i<imgs.length; i++){
                var mouseover = function(e){
                    engImg.src = this.src;
                    engImg.style.width = this.clientWidth*scale+'px';
                    engImg.style.height = this.clientHeight*scale+'px';
                    engImg.style.display = 'inline';
                    e?mousemove(e):mousemove();
                };
                var mousemove = function(e){
                    var offset = e$.getOffset(relObj);
                    var mouseX,mouseY,relX,relY;
                    if(e$.is_ie()){
                        mouseX = event.clientX + doc.body.scrollLeft+doc.documentElement.scrollLeft;
                        mouseY = event.clientY + doc.body.scrollTop +doc.documentElement.scrollTop;
                    }else{
                        mouseX = e.pageX;
                        mouseY = e.pageY;
                    }
                    relX = mouseX - offset.left;
                    relY = mouseY - offset.top;
                    engImg.style.top = (relY+20)+'px';
                    engImg.style.left = (relX+20)+'px';
                };
                var mouseout = function(e){
                    engImg.style.display = 'none';
                };
                imgs[i].onmouseover = mouseover;
                imgs[i].onmousemove = mousemove;
                imgs[i].onmouseout = mouseout;
	  
            }
        },
        hasInterface:function(name){
            if(e$[name] == undefined)return false;
            return true;
        },
        feedJSON:function(data){
            if(data && typeof data === 'object'){
                if(data.pass){
                    if(data.key && data.data && e$[data.key] && typeof e$[data.key] === 'function'){
                        e$[data.key](data);
                    }
                }
            }
        },
        setupPage:function(){
        },
        println:function(){
            var output=e$.getById('hyOutput');
            var str = "";
            for(var i=0; i<arguments.length; i++){
                str+=arguments[i]+"&nbsp;&nbsp;";
            }
            if(output){
                output.innerHTML +=str+'<br />';
            }
        },
        output:function(text){
            var o = e$.getById('hyOutput');
            if(o){
                o.innerHTML = text;
            }
        },
        Events:{
            ajaxBridgeReady:"ajaxbridgeready"
        },
        addEventListener:function(type,callback){
            if(eventStack[type]){
            }else{
                eventStack[type]=new Array();
            }
            eventStack[type].push(callback);
        },
        removeAllEventListener:function(type){
            if(eventStack[type])eventStack[type]=null;
        },
        removeEventListener:function(type,callback){
            if(eventStack[type]){
                var i = 0;
                while(i<eventStack[type].length){
                    if(eventStack[type][i] == callback){
                        eventStack[type].splice(i, 1);
                    }else{
                        i++;
                    }
                }
            }
        },
        trigger:function(type,param){
            if(eventStack[type]){
                var i= 0;
                while(i<eventStack[type].length){
                    var func = eventStack[type][i];
                    eventStack[type].splice(i, 1);
                    func(param);
                }
            }
        },
        willTrigger:function(type){
            if(eventStack[type] && eventStack[type].length>0)return true;
            return false;
        },
        animation:function(obj,params,speed,callback,delay){
            //e$.println(obj.id+":"+"animate"+":"+delay);
            if(!(params!=null && ((typeof params) =='object')))return false;
            speed = speed || 1000;
            delay = delay || 0;
            var anima = {};
            anima.object = obj;
            anima.speed = speed;
            anima.delay = delay;
            anima.callback = callback;
            anima.params = params;
            anima.state = 'pendding';
            if(!obj.hyUid){
                obj.hyUid = e$.getUniqueId();
            }
            if(!animateQueue[obj.hyUid]){
                animateQueue[obj.hyUid] = {};
                animateQueue[obj.hyUid].actions = [];
                animateQueue[obj.hyUid].state = 'active';
            }
            animateQueue[obj.hyUid].actions.push(anima);
            animateStart();
        },
        pauseAnimation:function(obj){
            if(obj.hyUid && animateQueue[obj.hyUid]){
                animateQueue[obj.hyUid].state = 'pause';
            }
            animateFinish();
        },
        resumeAnimation:function(obj){
            if(obj.hyUid && animateQueue[obj.hyUid]){
                animateQueue[obj.hyUid].state = 'active';
            }
            animateStart();
        },
        toggleAnimation:function(obj){
            if(obj.hyUid && animateQueue[obj.hyUid]){
                if(animateQueue[obj.hyUid].state == 'active'){
                    animateQueue[obj.hyUid].state = 'pause';
                    animateFinish();
                }else if (animateQueue[obj.hyUid].state == 'pause'){
                    animateQueue[obj.hyUid].state = 'active';
                    animateStart();
                }
            }
        },
        stopAnimation:function(obj,flag){
            if(obj.hyUid && animateQueue[obj.hyUid]){
                var acts = animateQueue[obj.hyUid].actions;
                var doact = function(act){
                    if(!act.params.length){
                        for(var name in act.params){
                            var value = act.params[name];
                            if(name =='left' || name== 'top' || name == 'width' || name == 'height'){
                                value +='px';
                            }
                            e$.setStyle(act.object, name, value);
                        }
                    }else if (act.params.length){
                        for(var j = 0; j<act.params.length; j++){
                            var cssObj = act.params[j];
                            e$.setStyle(act.object, cssObj.name, cssObj.pofix(cssObj.finish));
                        }
                    }
                }
                if(flag){
                    for (var i=0 ; i < acts.length; i++){
                        doact(acts[i]);
                        if(acts[i].callback){
                            acts[i].callback(acts[i].object);
                        }
                    }
                }else{
                    if(acts.length>0){
                        var act = acts[acts.length-1];
                        doact(act);
                        if(act.callback){
                            act.callback(act.object);
                        }
                    }
                }
                delete animateQueue[obj.hyUid];
                animateFinish();
            }
            
        },
        clearAnimation:function(obj){
            if(obj.hyUid && animateQueue[obj.hyUid]){
                delete animateQueue[obj.hyUid]
            }
            animateFinish();
        },
        setStyle:function(obj,params,value){
            var setcss = function(obj,name,value){
                if(name === 'opacity'){
                    e$.setOpacity(obj, value);
                }else{
                    obj.style[name] = value;
                }
            }
            if(arguments.length ===3 && typeof params ==='string'){
                setcss(obj,params,value)
            }else if (typeof params === 'object'){
                for (var name in params){
                    setcss(obj,name,params[name]);
                }
            }
        },
        getStyle:function(obj,name){
            if(name ==='opacity'){
                return e$.getComCss(obj, name);
            }else if(name === 'left'){
                return obj.offsetLeft+'px';
            }else if(name === 'top'){
                return obj.offsetTop+'px';
            }else if(name === 'width'){
                return obj.clientWidth+'px';
            }else if(name === 'height'){
                return obj.clientHeight+'px';
            }else{
                var res = obj.style[name];
                if(res === ''){
                    if(e$.ie){
                        res=e$.getComCss(obj, name);
                    }else{
                        res=e$.getComCss(obj, e$.cssItoM(name));
                    }
                }
                return res;
            }
            return false;
        },
        outerHeight:function(o){
            return o.offsetHeight;
        },
        outerWidth:function(o){
            return o.offsetWidth;
        },
        innerHeight:function(o){
            return o.clientHeight-e$.ph(o);
        },
        innerWidth:function(o){
            return o.clientWidth-e$.pw(o);
        },
        bw:function(o){
            var left,right;
            if(e$.ie){
                left = e$.getComCss(o, border[1]);
                if(left == 'medium')left = 0;
                else left = parseFloat(left);
                right = e$.getComCss(o, border[3]);
                if(right == 'medium')right = 0;
                else right = parseFloat(right);
            }else{
                left = parseFloat(e$.getComCss(o, borderAll[1]));
                right = parseFloat(e$.getComCss(o, borderAll[3]));
            }
            return left+right;
        },
        bh:function(o){
            var top,bottom;
            if(e$.ie){
                top = e$.getComCss(o, border[0]);
                if(top == 'medium')top = 0;
                else top = parseFloat(top);
                bottom = e$.getComCss(o, border[2]);
                if(bottom == 'medium')bottom = 0;
                else bottom = parseFloat(bottom);
            }else{
                top = parseFloat(e$.getComCss(o, borderAll[0]));
                bottom = parseFloat(e$.getComCss(o, borderAll[2]));
            }
            return top+bottom;
        },
        pw:function(o){
            var left,right;
            if(e$.ie){
                left = parseFloat(e$.getComCss(o, padding[1]));
                right = parseFloat(e$.getComCss(o, padding[3]));
            }else{
                left = parseFloat(e$.getComCss(o, paddingAll[1]));
                right = parseFloat(e$.getComCss(o, paddingAll[3]));
            }
            return left+right;
        },
        ph:function(o){
             if(e$.ie){
                return parseFloat(e$.getComCss(o, padding[0]))+parseFloat(e$.getComCss(o, padding[2]));
            }else{
                return parseFloat(e$.getComCss(o, paddingAll[0]))+parseFloat(e$.getComCss(o, paddingAll[2]));
            }
        },
        height:function(obj){
            return obj.clientHeight;
        },
        width:function(obj){
            return obj.clientWidth;
        },
        getComCss:function(obj,name){
            if(e$.ie){
                if(name === 'opacity'){
                    var filter = obj.currentStyle['filter'];
                    if(filter == ''){
                        return 1;
                    }else{
                        var res = ralpha.exec(filter)[1];
                        return res/100;
                    }
                }
                return obj.currentStyle[name];
            }else{
                return doc.defaultView.getComputedStyle(obj,null).getPropertyValue(name);
            }
        },
        cssItoM:function(text){
            return text.replace(itom, function(s){
                return '-'+s.toLowerCase()
            });
        },
        cssMtoI:function(text){
            return text.replace(mtoi,function(s0,s1){
                return s1.toUpperCase()
            });
        },
        getComCssObject:function(obj){
            if(e$.ie){
                return obj.currentStyle;
            }else{
                return doc.defaultView.getComputedStyle(obj,null);
            }
        },
        setOpacity:function(obj,value){
            if(e$.ie){
                var text = "alpha(opacity="+Math.floor(value*100)+")";
                obj.style.filter = text;
            }else{
                obj.style.opacity = value;
            }
        },
        extend:function(target,source){
            for(var name in source){
                target[name] = source[name];
            }
        },
        nextElem:function(obj){
            while(obj = obj.nextSibling){
                if(obj.nodeType && obj.nodeType == 1){
                    return obj;
                }
            }
            return false;
        },
        prevElem:function(obj){
            while(obj = obj.previousSibling){
                if(obj.nodeType && obj.nodeType == 1){
                    return obj;
                }
            }
            return false;
        },
        firstElem:function(obj){
            var elem = obj.firstChild;
            if(elem.nodeType){
                if(elem.nodeType == 1){
                    return elem;
                }else{
                    return e$.nextElem(elem);
                }
            }
            return false;
        },
        getClassName:function(obj){
            if(e$.ie){
                return obj.className;
            }else{
                return obj.getAttribute('class');
            }
        },
        setClassName:function(obj, className){
            if(e$.ie){
                obj.className = className;
            }else{
                obj.setAttribute('class',className);
            }
        },
        addClassName:function(obj,className){
            var name = e$.getClassName(obj)||'';
            e$.setClassName(obj, name+" "+className);
        },
        deleteClassName:function(obj,className){
            e$.setClassName(obj, e$.getClassName(obj).replace(className,''));
        }
    }
    
    var animateQueue={},animateTimer,animateState='stopped',interval=20;
    var doAnimate = function(){
        for(var name in animateQueue){
            if(animateQueue[name].actions.length==0){
                delete animateQueue[name];
                animateFinish();
            }
            else if(animateQueue[name].state == 'active'){
                var anim = animateQueue[name].actions[0];
                if(anim.state === 'pendding' && anim.delay > 0){
                    anim.delay -= interval;
                }else if(anim.state === 'pendding' && anim.delay<=0){
                    anim.state = 'started';
                    var params = anim.params;
                    anim.params = [];
                    for (var sty in params){
                        var orig = e$.getStyle(anim.object, sty);
                        var cssObj ={};
                        switch (sty){
                            case 'left':
                            case 'top':
                                e$.setStyle(anim.object, {
                                    position:'absolute',
                                    zIndex:1000
                                })
                            case 'width':
                            case 'height':
                                cssObj.pofix = function(value){
                                    return Math.floor(value) + 'px';
                                };
                                break;
                            case 'opacity':
                                cssObj.pofix = function(value){
                                    return Math.floor(value);
                                };
                                break;
                        }
                        cssObj.name = sty;
                        cssObj.origin = parseFloat(orig);
                        cssObj.finish = parseFloat(params[sty]);
                        if(sty == 'width'){
                            if(e$.ie){
                                cssObj.origin = anim.object.offsetWidth;
                                if(cssObj.finish ==0)cssObj.finish+=1;
                                //cssObj.finish = parseFloat(params[sty])+e$.pw(anim.object)+e$.bw(anim.object);
                            }else{
                                cssObj.origin = anim.object.clientWidth-e$.pw(anim.object);
                            }
                        }else if(sty == 'height'){
                            if(e$.ie){
                                cssObj.origin = anim.object.offsetHeight;
//                                cssObj.finish = parseFloat(params[sty])+e$.ph(anim.object)+e$.bh(anim.object);
                                if(cssObj.finish ==0)cssObj.finish+=1;
                            }else{
                                cssObj.origin = anim.object.clientHeight-e$.ph(anim.object);
                            }
                        }
                        cssObj.increment = parseFloat((cssObj.finish-cssObj.origin)/anim.speed*interval);
                        cssObj.current = cssObj.origin;
                        anim.params.push(cssObj);
                    }
                    
                    if(anim.params.length<=0){
                        if(anim.callback){
                            anim.callback(anim.object);
                        }
                        animateQueue[name].actions.shift();
                    }
                }else{
                    anim.speed-=interval;
                    if(anim.speed>0){
                        for(var i =0; i<anim.params.length ; i++){
                            cssObj = anim.params[i];
                            cssObj.current += cssObj.increment;
//                            if(cssObj.name=='height')e$.println(e$.getStyle(anim.object, 'height'),cssObj.pofix(cssObj.current),e$.getComCss(anim.object,'height'));
                            e$.setStyle(anim.object, cssObj.name, cssObj.pofix(cssObj.current));
                        }
                    }else if(anim.speed<=0){
                        for(var i =0; i<anim.params.length ; i++){
                            cssObj = anim.params[i];
//                            if(cssObj.name=='height')e$.println(e$.getStyle(anim.object, 'height'),cssObj.pofix(cssObj.finish),e$.getComCss(anim.object,'height'));
                            e$.setStyle(anim.object, cssObj.name, cssObj.pofix(cssObj.finish))
                        }
                        if(anim.callback){
                            anim.callback(anim.object);
                        }
                        animateQueue[name].actions.shift();
                    }
                }
                    
            }
        }
    }
    var animateStart=function(){
        if(animateState == 'stopped'){
            animateTimer = setInterval(doAnimate,interval);
            animateState = 'started';
        }
    }
    var animateFinish=function(){
        for ( var name in animateQueue){
            if(animateQueue[name].state == 'active'){
                return false;
            }
        }
        clearInterval(animateTimer);
        animateState = 'stopped';
        return true;
    }
    win.e$=ebayJSHelper;
})(window)

